package com.dstz.bpm.engine.action.cmd;

import com.dstz.base.api.exception.BusinessException;
import com.dstz.base.core.util.AppUtil;
import com.dstz.base.core.util.StringUtil;
import com.dstz.bpm.api.constant.ActionType;
import com.dstz.bpm.api.engine.action.cmd.BaseActionCmd;
import com.dstz.bpm.api.engine.action.cmd.FlowRequestParam;
import com.dstz.bpm.api.engine.action.cmd.TaskActionCmd;
import com.dstz.bpm.api.exception.BpmStatusCode;
import com.dstz.bpm.api.exception.WorkFlowException;
import com.dstz.bpm.api.model.task.IBpmTask;
import com.dstz.bpm.engine.action.handler.AbsActionHandler;
import com.dstz.bpm.engine.constant.TaskSkipType;
import java.util.Map;
import org.activiti.engine.delegate.DelegateTask;

public class DefualtTaskActionCmd extends BaseActionCmd implements TaskActionCmd {
   private String taskId;
   private IBpmTask az;
   private DelegateTask aA;
   private TaskSkipType aB;

   public DefualtTaskActionCmd() {
      this.aB = TaskSkipType.NO_SKIP;
   }

   public DefualtTaskActionCmd(FlowRequestParam flowParam) {
      super(flowParam);
      this.aB = TaskSkipType.NO_SKIP;
   }

   @Override
   public String getTaskId() {
      return this.az != null ? this.az.getId() : this.taskId;
   }

   public void setTaskId(String taskId) {
      this.taskId = taskId;
   }

   @Override
   public void initSpecialParam(FlowRequestParam flowParam) {
      String taskId = flowParam.getTaskId();
      if (StringUtil.isEmpty(taskId)) {
         throw new BusinessException("taskId 不能为空", BpmStatusCode.TASK_NOT_FOUND);
      } else {
         this.setTaskId(taskId);
         this.setDestination(flowParam.getDestination());
      }
   }

   @Override
   public IBpmTask getBpmTask() {
      return this.az;
   }

   @Override
   public void setBpmTask(IBpmTask task) {
      this.az = task;
   }

   public DelegateTask getDelagateTask() {
      if (this.aA == null) {
      }

      return this.aA;
   }

   public void setDelagateTask(DelegateTask task) {
      this.aA = task;
   }

   @Override
   public String getNodeId() {
      return this.az.getNodeId();
   }

   @Override
   public void addVariable(String variableName, Object value) {
      if (this.aA == null) {
         throw new WorkFlowException(BpmStatusCode.VARIABLES_NO_SYNC);
      } else {
         this.aA.setVariable(variableName, value);
      }
   }

   @Override
   public Object getVariable(String variableName) {
      if (this.aA == null) {
         throw new WorkFlowException(BpmStatusCode.VARIABLES_NO_SYNC);
      } else {
         return this.aA.getVariable(variableName);
      }
   }

   @Override
   public boolean hasVariable(String variableName) {
      if (this.aA == null) {
         throw new WorkFlowException(BpmStatusCode.VARIABLES_NO_SYNC);
      } else {
         return this.aA.hasVariable(variableName);
      }
   }

   @Override
   public void removeVariable(String variableName) {
      if (this.aA == null) {
         throw new WorkFlowException(BpmStatusCode.VARIABLES_NO_SYNC);
      }
   }

   @Override
   public Map<String, Object> getVariables() {
      return this.aA.getVariables();
   }

   public synchronized String b() {
      if (this.hasExecuted) {
         throw new BusinessException("action cmd cannot be invoked twice", BpmStatusCode.PARAM_ILLEGAL);
      } else {
         this.hasExecuted = true;
         ActionType actonType = ActionType.fromKey(this.getActionName());
         AbsActionHandler handler = (AbsActionHandler)AppUtil.getBean(actonType.getBeanId());
         if (handler == null) {
            throw new BusinessException("action beanId cannot be found :" + actonType.getName(), BpmStatusCode.NO_TASK_ACTION);
         } else {
            handler.g(this);
            return handler.getActionType().getName();
         }
      }
   }

   public TaskSkipType c() {
      return this.aB;
   }

   public void setHasSkipThisTask(TaskSkipType isSkip) {
      this.aB = isSkip;
   }
}
